package com.didi.gulimall.seckill.controller;

import com.alibaba.fastjson.JSON;
import com.didi.gulimall.common.exception.DuplicateFieldsException;
import com.didi.gulimall.common.to.mq.SeckillOrderTo;
import com.didi.gulimall.common.utils.LogUtils;
import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.seckill.service.SecKillService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @Author 10626
 * @Create 2021/6/14 17:52
 */
@Api("秒杀")
@Controller
@Slf4j
public class SecKillController {

    @Value("${gulimall.item}")
    private String itemHtml;

    @Value("${gulimall.seckill}")
    private String seckillHtml;


    @Autowired
    private SecKillService secKillService;

    @ResponseBody
    @GetMapping("get/seckill")
    public R getSecKill() {
        return secKillService.getSecKill();
    }

    @ResponseBody
    @GetMapping("get/seckill/{skuId}")
    public R getSecKillBySkuId(@PathVariable("skuId") Long skuId) {
        return secKillService.getSecKillBySkuId(skuId);
    }

    @GetMapping("get/seckill/order")
    public String getSecKillOrder(@RequestParam("skuId") Long skuId,
                                  @RequestParam("orderSn") String orderSn,
                                  @RequestParam("memberId") Long memberId,
                                  Model model) {
        SeckillOrderTo skill = secKillService.getSecKillOrder(orderSn, memberId);
        model.addAttribute("seckill", skill);
        return "success";
    }
    @ResponseBody
    @GetMapping("order/seckill/member/list")
    public R getSecKillMemberList() {
        return secKillService.getSecKillMemberList();


    }

    /**
     * @return
     */
    @GetMapping("kill")
    public String skill(
            @RequestParam("sessionId") String sessionId,
            @RequestParam("key") String key,
            @RequestParam("num") Integer num,
            @RequestParam("skuId") Long skuId,
            RedirectAttributes redirectAttributes) {
        try {
            SeckillOrderTo skill = secKillService.skill(sessionId, key, num, skuId);
            log.info("返回数据{}", JSON.toJSONString(skill));
            redirectAttributes.addAttribute("skuId", skill.getSkuId());
            redirectAttributes.addAttribute("orderSn", skill.getOrderSn());
            redirectAttributes.addAttribute("memberId", skill.getMemberId());
            return "redirect:" + seckillHtml + "get/seckill/order";
        } catch (DuplicateFieldsException e) {
            log.error("出错了{}", LogUtils.getStackTraceInfo(e));
            return "redirect:" + itemHtml + skuId + ".html";
        }
    }
}
